package br.com.fiap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.fiap.bean.Cliente;
import br.com.fiap.cfactory.ConnectionFactory;



public class ClienteDAO {

	Connection conn = null;
	
	public ClienteDAO(){
		
		try{
			conn = ConnectionFactory.getConnection();
		}
		catch(SQLException ex){ }
	}
	
	public boolean validaAgCc(Cliente c){

		String sql = "SELECT * FROM tb_cliente WHERE agencia = ? and conta = ?";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, c.getAgencia());
			stmt.setInt(2, c.getConta());
					
			ResultSet rs = stmt.executeQuery();
			
			if(rs.next()){
				c.setSaldo(rs.getDouble("saldo"));
				c.setId_cliente(rs.getInt("id_cliente"));
				c.setNome(rs.getString("nome"));
				return true;
			}
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
		return false;
	}
	
	public boolean validaLogin(Cliente c){

		String sql = "SELECT * FROM tb_cliente WHERE senha = ? and agencia = ? and conta = ? ";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, c.getSenha());
			stmt.setInt(2, c.getAgencia());
			stmt.setInt(3, c.getConta());
								
			ResultSet rs = stmt.executeQuery();
			
			if(rs.next()){
				return true;
			}
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
		
		return false;
	}

	public void getSaldo(Cliente c) {
		
		String sql = "SELECT saldo FROM tb_cliente WHERE agencia = ? and conta = ? ";
		
		try{
			PreparedStatement stmt = conn.prepareStatement(sql);
			
			stmt.setInt(1, c.getAgencia());
			stmt.setInt(2, c.getConta());
								
			ResultSet rs = stmt.executeQuery();
			
			if(rs.next()){
			c.setSaldo(rs.getDouble("saldo"));
			}
		}
		catch(SQLException ex){ 
			ex.printStackTrace();
		}
		finally{
			
		}
				
	}

}
